package com.example.hasee.zhcd.Adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.hasee.zhcd.Activities.ChuanWu.LPEntrustActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.LPInnerActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.ShipDetailActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.WayBillActivity;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by hasee on 2017/9/13.
 */

public class LeavePortAdapter extends BaseAdapter {
    private HashMap<String, String> map = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private ArrayList<HashMap<String, String>> list;
    private LayoutInflater itemInflater;
    private Context context;
    private Holder holder;

    public LeavePortAdapter(Context context, ArrayList<HashMap<String, String>> list) {
        this.context = context;
        this.list = list;
        this.itemInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new Holder();
            convertView = itemInflater.inflate(R.layout.leaveport_list_item, null);
            holder.shipname = (TextView) convertView.findViewById(R.id.shipname);
            holder.shiporder = (TextView) convertView.findViewById(R.id.shiporder);
            holder.company = (TextView) convertView.findViewById(R.id.company);
            holder.leavetime = (TextView) convertView.findViewById(R.id.leavetime);
            holder.entrust = (TextView) convertView.findViewById(R.id.fwbsx);
            holder.detail = (TextView) convertView.findViewById(R.id.detail);
            holder.line = (View) convertView.findViewById(R.id.line);
            holder.linear = (LinearLayout) convertView.findViewById(R.id.linear);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
//{"fshipcode":"10457", "fshipname":"天台97", "fshipordernum":"1811", "fzygs":"杂货", "flgdate":"06-14 09:10",
// "fwbsx":"", "fcgs":"wldw_11815", "fshipyearnumvcr":"QHD201802486"}
        holder.shipname.setText((String) list.get(position).get("fshipname"));
        holder.shipname.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        holder.shipname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ShipDetailActivity.class);
                intent.putExtra("strSYN", list.get(position).get("fshipyearnumvcr"));
                context.startActivity(intent);
            }
        });
        holder.shiporder.setText((String) list.get(position).get("fberth"));
        holder.company.setText((String) list.get(position).get("fzygs"));
        holder.company.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        if (list.get(position).get("fzygs").length() != 0) {
            holder.company.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, WayBillActivity.class);
                    intent.putExtra("strSYN", list.get(position).get("fshipyearnumvcr"));
                    context.startActivity(intent);
                }
            });
        }
        holder.leavetime.setText((String) list.get(position).get("flgdate"));
        holder.linear.setVisibility(View.VISIBLE);
        holder.line.setVisibility(View.VISIBLE);

        if (!"".equals((String) list.get(position).get("fwbsx"))) {
            holder.entrust.setVisibility(View.VISIBLE);
            holder.entrust.setText("委办" + (String) list.get(position).get("fwbsx") + ">>");
            holder.entrust.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context, LPEntrustActivity.class);
                    intent.putExtra("fshipcode", list.get(position).get("fshipcode"));
                    intent.putExtra("fshipyearnumvcr", list.get(position).get("fshipyearnumvcr"));
                    context.startActivity(intent);

                }
            });
        } else {
            holder.entrust.setVisibility(View.INVISIBLE);
        }
        holder.detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LPInnerActivity.class);
                intent.putExtra("fshipyearnumvcr", list.get(position).get("fshipyearnumvcr"));
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class Holder {
        private TextView shipname;
        private TextView shiporder;
        private TextView company;
        private TextView leavetime;
        private TextView entrust;
        private TextView detail;
        private LinearLayout linear;
        private View line;
    }
}
